﻿/* Addon Nice editor translate tool */
google.load("language", "1");
function OnClientCommandExecuting(editor, args) {
    if (args.get_name() == "TranslateTool") {
        //Get language
        var language = args.get_value();
        var hasSelection = true;
        var html = editor.getSelectionHtml().trim(); 
        if (!html) {
            html = editor.get_html(true); 
            hasSelection = false;
        }
        google.language.detect(html, function(result) {
            if (!result.error && result.language) {
                google.language.translate(html, result.language, language, function(result2) {
                    if (!result2.error) {
                        currentSelectedHtml = result2.translation;
                        if (hasSelection) {
                            editor.pasteHtml(currentSelectedHtml);
                        }
                        else editor.set_html(currentSelectedHtml);
                    }
                });
            }
        });
        args.set_cancel(true);
    }
}
